from django.contrib.auth.models import User
from django.db import models

# Create your models here.
from django.db.models import Q

from UtilsDir.BaseModel import BaseModel


class imgInfoManager(models.Manager):
    def get_default_img(self, user):
        try:
            imgInfo_list = self.filter(Q(upload_by=user))
        except self.model.DoesNotExist:
            imgInfo_list = None
        return imgInfo_list

    def get_all_img(self):
        try:
            imgInfo_list = self.all()
        except self.model.DoesNotExist:
            imgInfo_list = None
        return imgInfo_list


class imgInfo(BaseModel):
    id = models.CharField('主键', max_length=100, primary_key=True)
    file_name = models.CharField("文件名称", max_length=50, null=True, blank=True)
    file_path = models.CharField("文件路径", max_length=1000)
    upload_by = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
    img_type = models.CharField('图片类型', max_length=64, null=True, blank=True)

    objects = imgInfoManager()

    class Meta:
        db_table = 'dj_upload_image'
        verbose_name = '上传图片'
        verbose_name_plural = verbose_name


class SplicingInfo(BaseModel):
    id = models.CharField('主键', max_length=100, primary_key=True)
    splicing_col = models.SmallIntegerField("横向拼接数")
    splicing_row = models.SmallIntegerField("纵向拼接数")
    main_img_col = models.SmallIntegerField("列数")
    main_img_row = models.SmallIntegerField("行数")
    progress = models.SmallIntegerField("拼接进度")
    end_time = models.DateTimeField("完成时间", null=True, blank=True)
    splicing_img = models.ForeignKey(imgInfo, on_delete=models.CASCADE, null=True, blank=True)

    class Meta:
        db_table = 'dj_splicing_info'
        verbose_name = '拼接信息'
        verbose_name_plural = verbose_name


class SplicingImgInfoManager(models.manager):
    def getByRow(self, sInfoId, row):
        try:
            info_list = self.filter(Q(img_row=row), Q(splicing_info=sInfoId)).order_by('img_col')
        except self.model.DoesNotExist:
            info_list = None
        return info_list


class SplicingImgInfo(BaseModel):
    id = models.CharField('主键', max_length=100, primary_key=True)
    img_col = models.SmallIntegerField("列数")
    img_row = models.SmallIntegerField("行数")
    splicing_info = models.ForeignKey(SplicingInfo, on_delete=models.CASCADE)
    img_info = models.ForeignKey(imgInfo, on_delete=models.CASCADE)

    objects = SplicingImgInfoManager()

    class Meta:
        db_table = 'dj_splicing_and_img'
        verbose_name = '拼接图片信息'
        verbose_name_plural = verbose_name

